<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button type="primary" :disabled="isaddress" @click="addschoolclassroom()">添加教室</a-button>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-form :inline="true" size="mini">
          <el-form-item label="校区">
            <el-select v-model="search" placeholder="校区">
              <el-option label="全部校区" value></el-option>
              <el-option
                v-for="(item, i) in getschooladdress"
                :key="`2inline${i}`"
                :label="item.name"
                :value="item.ID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-table
          :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
          style="width: 100%; border: 1px solid #eeeeee"
        >
          <el-table-column label="序号" width="80">
            <template slot-scope="scope">{{scope.$index+1}}</template>>
          </el-table-column>
          <el-table-column prop="name" label="教室名称" width="180"></el-table-column>
          <el-table-column prop="type" sortable label="所属校区">
            <template
              slot-scope="scope"
            >{{ getschooladdressbyid( scope.row.schooladdressid).name}}</template>
          </el-table-column>
          <el-table-column prop="info" label="简介"></el-table-column>
          <el-table-column prop="studentnum" sortable label="容纳人数"></el-table-column>
          <el-table-column align="right" width="200px">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="addschoolclassroom(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="delschoolclassroom(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          @current-change="currentChange"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="tableData.length"
        ></el-pagination>
        <el-dialog class="school-form" :title="title" width="510px" :visible.sync="setaddress">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini">
            <el-form-item label="教室名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>

            <el-form-item label="所属校区" prop="schooladdressid">
              <el-select v-model="ruleForm.schooladdressid" allow-create placeholder="请选择校区">
                <el-option
                  v-for="(item, i) in getschooladdress"
                  :key="`1classroom${i}`"
                  :label="item.name"
                  :value="item.ID"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="容纳人数">
              <el-input-number v-model="ruleForm.studentnum" :min="1" :max="500" label="容纳人数"></el-input-number>
            </el-form-item>
            <el-form-item label="简介">
              <el-input v-model="ruleForm.info"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
            <el-button @click="setaddress = false" size="mini">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asynccreatclassroom,
  asyncdelclassroom
} from "../../../../plugins/api.js";
export default {
  name: "classroom",
  data() {
    return {
      formInline: "",
      isaddress: true,
      setaddress: false,
      search: "",
      currentPage: 1,
      pagesize: 10,
      title: "添加教室",
      ruleForm: {
        schooladdressid: "",
        name: "",
        info: "",
        studentnum: 10
      },
      rules: {
        name: [
          { required: true, message: "请输入教室名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" }
        ],
        schooladdressid: [
          { required: true, message: "请选择校区", trigger: "change" }
        ],
        studentnum: [
          { required: true, message: "请输入容纳人数", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["getschoolclassroom", "getschooladdress", "getschooladdressbyid"]),
    tableData() {
      const { getschoolclassroom, search } = this;
      return getschoolclassroom.filter(data => {
        if (search) {
          return data.schooladdressid == search;
        }
        return true;
      });
    }
  },
  mounted() {
    if (this.getschooladdress.length) {
      this.isaddress = false;
    }
  },
  methods: {
    currentChange(index) {
      this.currentPage = index;
    },
    addschoolclassroom(v) {
      this.setaddress = true;
      if (v) {
        this.title = "修改教室";
        this.ruleForm = JSON.parse(JSON.stringify(v));
      } else {
        this.title = "添加教室";
        const schooladdressid = this.getschooladdress.find(() => true).ID;
        this.ruleForm = {
          schooladdressid,
          name: "",
          info: "",
          studentnum: 10
        };
      }
    },
    delschoolclassroom(ID) {
      this.$confirm("此操作将永久删除该教室, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            await asyncdelclassroom({ ID });
            await this.$store.dispatch("actionschoolclassroom");

            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async submitForm() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          if (this.$store.state.state) return;
          this.$store.commit("setstate", true);
          const data = this.ruleForm.ID ? "修改" : " 添加";
          try {
            await asynccreatclassroom(this.ruleForm);
            await this.$store.dispatch("actionschoolclassroom");
            this.setaddress = false;
            this.$message.success(`${data}成功`);
          } catch (err) {
            this.$message.error(`${data}失败`);
          }
          this.$store.commit("setstate", false);
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
.dialog-footer {
  text-align: center;
}
</style>